import wxImg from "@/assets/images/qr_wxq.png";
import { flattenDeep } from 'lodash-es'
import { regexpEmail, regexpURL } from '@/utils/regex'

export function getWeChatImg() {
  return wxImg;
}
export const kefuUrl = "https://work.weixin.qq.com/kfid/kfc7e404633cf3962fb";

/**
 * 文本翻译支持的语言列表
 * */
export const supportTranLanguage2 = [
  {
    gid: 'en',
    id: 'en',
    title: '英语'
  },
  {
    gid: 'zh',
    id: 'zh',
    title: '中文(简体)'
  },
  {
    gid: 'ja',
    id: 'jp',
    title: '日语'
  },
  {
    gid: 'ko',
    id: 'kor',
    title: '韩语'
  },
  {
    gid: 'fr',
    id: 'fra',
    title: '法语'
  },
  {
    gid: 'de',
    id: 'de',
    title: '德语'
  },
  {
    gid: 'es',
    id: 'spa',
    title: '西班牙语'
  },
  {
    gid: 'it',
    id: 'it',
    title: '意大利语'
  },
  {
    gid: 'pt',
    id: 'pt',
    title: '葡萄牙语'
  },
  {
    gid: 'vi',
    id: 'vie',
    title: '越南语'
  },
  {
    gid: 'th',
    id: 'th',
    title: '泰语'
  },
  {
    gid: 'ar',
    id: 'ara',
    title: '阿拉伯语'
  },
  {
    gid: 'ru',
    id: 'ru',
    title: '俄语'
  },
  {
    gid: 'el',
    id: 'el',
    title: '希腊语'
  },
  {
    gid: 'nl',
    id: 'nl',
    title: '荷兰语'
  },
  {
    gid: 'pl',
    id: 'pl',
    title: '波兰语'
  },
  {
    gid: 'da',
    id: 'dan',
    title: '丹麦语'
  },
  {
    gid: 'sv',
    id: 'swe',
    title: '瑞典语'
  }
]

/**
 * 获取文本语言
 * @param {String} text
 * @returns {String} language
 */
export function getTextLanguage(text = '') {
  if (typeof text !== 'string') {
    text = ''
  }
  var zhStrLen = text.match(/[\u4e00-\u9fa5]/g)
  var contentVal = text.replace(/[\d\s]/g, '')
  if (zhStrLen && zhStrLen.length >= contentVal.length / 2) {
    return 'en'
  }
  return 'zh'
}

/**
 * 检查引擎是否支持指定语言
 * */
export function checkEngineSupportsLang(engine: string, lang: string){
  if(!lang || !engine) return true
  return supportTranLanguage2.some(({ gid }) => gid === lang)
}

/**
 * 获取单词语言
 * @param {String} text
 * @returns {String|undefined} language
 */
export function getWordsLanguage(text = '') {
  if (typeof text !== 'string') {
    return
  }
  text = text.trim()
  if (text.match(/^[\u4e00-\u9fa5]{1,6}$/)) {
    return 'zh'
  }
  if (text.match(/^([a-zA-Z-'.$&(0-9]{1,58}[\s]?){1,3}$/i)) {
    return 'en'
  }
}
/**
 * 转换客户端传入的payload数据
 */
export function adaptPayloadParam(str: any){
  let params
  try {
    params = typeof str === 'string' ? JSON.parse(str) : str
  }catch (e) {
    params = null
  }
  return params
}

/**
 * 是否可以收藏到单词本或拆长句
 * @param {Number} param0.text 文本
 * @param {String} param0.action 行为 collect 收藏  break 拆长句
 * @param {String} param0.text 文本
 * @returns {Boolean} 是否合法
 */
export function canActionByEnglish(option: any){
  const { text, action = 'collect', sLang, tLang } = option
  if (!['auto', 'en'].includes(sLang) || tLang !== 'zh') {
    return false
  }

  const params: any = {
    text
  }
  if(action === 'collect'){
    params['wordLength'] = 1
  }else{
    params['wordMinLength'] = 2
  }
  return textIsEnglish(params)
}

/**
 * 判断是否属于合法英文
 * @param {Number} param0.wordMinLength 单词最少个数，当wordLength>0时该参数无效
 * @param {Number} param0.wordLength 单词个数
 * @param {String} param0.text 文本
 * @returns {Boolean} 是否合法
 */
function textIsEnglish(option: any){
  const { wordMinLength = 0, wordLength = 0 } = option
  let text = option.text
  text = text.trim()
  if(!text) {
    return false
  }

  const texts = splitByEmptySymbol(text.replace(/(\n)+/g, ' '))
  if(wordLength > 0){
    if (texts.length !== wordLength) {
      return false
    }
  }else{
    if (texts.length < wordMinLength) {
      return false
    }
  }

  const chineseSymbols = /，|；|。|‘|’|“|”|—|–/g
  const englishSymbols = /,|;|.|'|'|"|"|-|-/g
  const ligTexts = texts
    // .filter(text => /^[a-zA-Z]+(.)*$/.test(text))
    .map((text: any) => {
      return text.replaceAll(/^[,|$|.|;|:|)|(|'|/|"|%|#|&|*|-]*|[,|$|.|;|:|)|(|'|/|"|%|#|&|*|-]*$/g, "")
        .replace(chineseSymbols, (match: any) => match.replaceAll(englishSymbols, ''))
        .replace(/^[0-9]*$/, '')
        .replace(/(\n)+/g, ' ')
        .replace(/(\"|\')+/g, '')
    }).filter(i => i)

  return ligTexts.every(t => {
    return getWordsLanguage(t) === 'en' || regexpEmail(t) || regexpURL(t)
  })
}

function splitByEmptySymbol (text: any) {
  const emptySymbolEn = ' '
  const emptySymbolCh = ' '
  const len1 = text.split(emptySymbolEn).length
  const len2 = text.split(emptySymbolCh).length
  let texts
  if (len1 > len2) {
    texts = text.split(emptySymbolEn)
  } else {
    texts = text.split(emptySymbolCh)
  }

  if (!texts.length) {
    return []
  }

  return flattenDeep(texts.map((i: any) => {
    const len = Math.max(i.split(emptySymbolEn).length, i.split(emptySymbolCh).length)
    if (len === 1) {
      return i
    }

    return splitByEmptySymbol(i)
  }))
}
